package amazon;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;


/**
 * Created by richard on 5/29/14.
 */
public class Island {
    @Test
    public void testIsland(){
        int[] numbers= {9,0,0,0,0,0,0,0,0,0,3,3};
        int[] numberResult={0,0,0,0,0,0,0,0,0};
        int[] numbers1={12,0,1,0,0,0,1,1,0,0,1,0,0,4,3};
        int[] numbers1Result={0,1,1,0,0,1,1,0,0,1,1,0};
        int[] numbers2={20,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,0,4,5};
        int[] numbers2Result={0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0,0,1,1,0};


        assertArrayEquals(generateFilledIsland(numbers), numberResult);
        assertArrayEquals(generateFilledIsland(numbers2),numbers2Result);

        assertArrayEquals(generateFilledIsland(numbers1),numbers1Result);
    }

    public static int[] generateFilledIsland(int[] numbers){
        int length=numbers[numbers.length-2];
        int width=numbers[numbers.length-1];
        int index=0;
        for(int i =0; i < length;i++){

            for(int j =0; j<width;j++){
                index++;
                if(ifAroundWithIsland(numbers,length,width,index)){
                    numbers[index]=1;
                }
            }
        }
        return Arrays.copyOfRange(numbers,1,numbers.length-2);

    }

    public static boolean ifAroundWithIsland(int[] numbers,int length,int width,int index){
        int left=index-1;
        int right=index+1;
        int down=index+length;
        int up=index-length;
        int upRight = up+1,upLeft=up-1,downRight=down+1,downLeft=down-1;
        int sum =0;
        boolean around =false;
       if(up>0){
           if(left>0&&(left%length==(index%length)-1)){
               if(numbers[left]==1){
                   sum++;
               }
           }
           if(upLeft>0&&(upLeft%length==(index%length)-1)){
               if(numbers[upLeft]==1){
                   sum++;
               }
           }
           if(numbers[up]==1){
               sum++;
           }
           if(sum==3){
               return true;
           }
       }
        sum=0;
        if(up>0){
            if(right<numbers.length&&(right%length==(index%length)+1)){
                if(numbers[right]==1){
                    sum++;
                }
            }
            if(upRight<numbers.length&&(upRight%length==(index%length)+1)){
                if(numbers[upRight]==1){
                    sum++;
                }
            }
            if(numbers[up]==1){
                sum++;
            }
            if(sum==3){
                return true;
            }
        }
        sum=0;
        if(down <numbers.length){
            if(left>0&&(left%length==(index%length)-1)){
                if(numbers[left]==1){
                    sum++;
                }
            }
            if(downLeft>0&&(downLeft%length==(index%length)-1)){
                if(numbers[downLeft]==1){
                    sum++;
                }
            }
            if(numbers[down]==1){
                sum++;
            }
            if(sum==3){
                return true;
            }
        }
        sum=0;
        if(down <numbers.length){
            if(right<numbers.length&&(right%length==(index%length)+1)){
                if(numbers[right]==1){
                    sum++;
                }
            }
            if(downRight<numbers.length&&(downRight%length==(index%length)+1)){
                if(numbers[downRight]==1){
                    sum++;
                }
            }
            if(numbers[down]==1){
                sum++;
            }
            if(sum==3){
                return true;
            }
        }
        return false;

    }
}
